package com.example.shop.service.impl;

import com.example.shop.mapper.OrderMapper;
import com.example.shop.model.CommodityType;
import com.example.shop.model.OrderForm;
import com.example.shop.service.OrderService;
import com.example.shop.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    private PageBean<OrderForm> pageBean;

    @Override
    public int insertOrder(OrderForm orderForm) {
        return orderMapper.insertOrder(orderForm);
    }

    @Override
    public PageBean<OrderForm> getPageBean(int uId, int pageNo, int pageSize) {
        int dataCount = orderMapper.getCount(uId);
        pageBean = new PageBean<>(dataCount, pageSize);
        pageBean.setPageNo(pageNo);
        int page = (pageNo - 1) * pageSize;
        pageBean.setDataList(orderMapper.getPageBean(uId, page, pageSize));
        System.out.println(pageBean);
        return pageBean;
    }

    @Override
    public int deleteById(Integer id) {
        return orderMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageBean<OrderForm> getOrdersForms(int pageNo, int pageSize, String search) {
        pageBean = new PageBean<>();
        pageBean.setPageSize(pageSize);
        pageBean.setDataCount(orderMapper.getDataCount());
        pageBean.setPageNo(pageNo);
        if ("".equals(search) || search.length() == 0) {
            search = null;
        }
        List<OrderForm> users = orderMapper.getOrdersForms((pageNo - 1) * pageSize, pageSize, search);
        pageBean.setDataList(users);
        return pageBean;
    }
}
